 # nothing here yet
import pytest
import tasks
from tasks import Task

# 为了使用MongoDB，需要将所有测试的db_type都指向mongo

# @pytest.fixture(scope='session', params=['tiny',])
@pytest.fixture(scope='session', params=['tiny','mongo'])
def tasks_db_session(tmpdir_factory, request): # tmdir的作用范围是function，mpdir_factory是session
    """
    测试之前链接数据库，测试之后断开
    :param tmpdir_factory:
    :return:
    """
    temp_dir = tmpdir_factory.mktemp('temp')
    tasks.start_tasks_db(str(temp_dir), request.param)
    yield
    tasks.stop_tasks_db()

@pytest.fixture()
def tasks_db(tasks_db_session):#tmpdir作用范围是function，修改tasks_db_session，会话级别是session
    """
    测试之前链接数据库，之后断开
    :param tmpdir:
    :return:
    """
# #     setup：start db
#     tasks.start_tasks_db(str(tmpdir),'tiny')
#     yield # 测试执行时入口，之前的为预置数据，之后的为清理工作
# #     Teardown ：stop db
#     tasks.stop_tasks_db()
    tasks.delete_all()

# Task接口
# Task(summary=None, owner=None, done=False, id=None)
# summary是必须的
# owner和done是可选的
# id是被数据库设置的
@pytest.fixture(scope='session') # 修改作用范围scope='session'
def tasks_just_a_few():
    """
    全部summary和owners是独一无二的
    :return:
    """
    return (
        Task('Write somecode', 'Brian', True),
        Task("Code review Brian's code", 'katie', False),
        Task('Fix what Brain did', 'Michelle', False)
    )

@pytest.fixture(scope='session')
def tasks_mult_per_owner():
    """
    每个用用者有多个任务
    :return:
    """
    return (
        Task("make a cookie", 'Raphael'),
        Task("Use an emoji", "Raphael"),
        Task("Move to BerLin", 'Raphael'),

        Task('Create', 'Michelle'),
        Task('Inspire', 'Michelle'),
        Task('Encourage', 'Michelle'),

        Task('Do a handstand', 'Daniel'),
        Task('Write some books', 'Daniel'),
        Task('Eat ice cream', "Daniel")
    )

@pytest.fixture()
def db_with_3_tasks(tasks_db,tasks_just_a_few):
    """
    使用3个tasks链接数据库,独一无二
    :param tasks_db:
    :param tasks_just_a_few:
    :return:
    """
    for t in tasks_just_a_few:
        tasks.add(t)

@pytest.fixture()
def db_with_multi_per_owner(tasks_db,tasks_mult_per_owner):
    """
    使用9个任务链接数据库，3个owner，所有的都有3个任务
    :param tasks_db:
    :param tasks_mult_per_owner:
    :return:
    """
    for t in tasks_mult_per_owner:
        tasks.add(t)

